/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
$(document).ready(function() {
    getData();

    $('.cadastro').submit(function(event) {
        event.preventDefault();
        sendData();

    });
    $('.search').click(function(){
        var f = $("form").serialize();
        buscaDataOne(f);
    });

    $("#alerta").dialog({
        autoOpen: false,
        modal: true,
        buttons: {
            Ok: function() {
                $(this).dialog("close");
            }
        }
    });

    function val() {
        $("#tel").mask("(99) 9999-9999");
        $("#cpf").mask("999.999.999-99");
    }

    function _error(data) {
        console.log(data);
        alert(data);
    }

    function sendData() {

        var data = $("form").serialize() + "&perfil=Medico";
       // alert(data);
        $.ajax({
            type: "POST",
            dataType: 'json',
            url: "PessoaSev",
            data: data + "&action=add",
            success: function(data) {
                $("#alerta").html(data.erro);
                $("#alerta").dialog("open");
                $('.cadastro :input[type!=submit] :input[type!=submit] ').val('');
                $("form").get(0).reset();
                getData();
            }
        });

    }
    function getData() {
        val();
        var parseData = function (data) {
            if (typeof (data.erro) === 'undefined') {
                delAllLinha();
                $.each(data, function (index, element) {
                    $("#mtable tbody").append("<tr><td >" + element.cpf + "</td><td>" + element.nome + "</td><td>" + element.email + "</td><td>" + element.cidade + "</td><<td  id='del'> <img n='" + element.id + "' src='img/delete.png' width='16' height='16'/><img n='" + element.id + "' src='img/edit.png' width='16' height='16'/> </td>");
                    $("tr:odd").addClass("odd");
                    $("tr:even").addClass("even");
                });
                loadelement();
            } else {
                $("#alerta").html(data.erro);
                $("#alerta").dialog("open");
            }
        }
        $.ajax({
            type: "POST",
            dataType: 'json',
            url: "PessoaSev",
            data: "action=list&perfil=Medico",
            success: parseData,
            error: _error
        });

    }
        function buscaDataOne(d) { 
            
        $.ajax({
            type: "POST",
            dataType: 'json',
            url: "PessoaSev",
            data: "action=searchmedico&perfil=Medico&"+d,
            success: function(data) {
                
                if (typeof (data.erro) === 'undefined') {
                    delAllLinha();
                    $.each(data, function(index, element) {
                        $("#mtable tbody").append("<tr><td >" + element.cpf + "</td><td>" + element.nome + "</td><td>" + element.email + "</td><td>" + element.cidade + "</td><<td  id='del'> <img n='" + element.id + "' src='img/delete.png' width='16' height='16'/><img n='" + element.id + "' src='img/edit.png' width='16' height='16'/> </td>");
                        $("tr:odd").addClass("odd");
                        $("tr:even").addClass("even");
 
                    });
                    loadelement();
                    val();
                } else {

                    $("#alerta").html(data.erro);
                    $("#alerta").dialog("open");
                }
            },
            error: function(data) {
                Alert(data);
            }
        });

        $('img').click(function() {
            alert("");
        });
    }
    function getDataOne(id) {    
        $.ajax({
            type: "POST",
            dataType: 'json',
            url: "PessoaSev",
            data: "action=one&id="+id,
            success: function(data) {
                if (typeof (data.erro) === 'undefined') {
                    delAllLinha();
                   // $.each(data, function(index, element) {
                        $("#mtable tbody").append("<tr><td >" + data.cpf + "</td><td>" + data.nome + "</td><td>" + data.email + "</td><td>" + data.cidade + "</td><<td  id='del'> <img n='" + data.id + "' src='img/delete.png' width='16' height='16'/><img n='" + data.id + "' src='img/edit.png' width='16' height='16'/> </td>");
                        $("input[name='nome']").val(data.nome);
                        $("input[name='cpf']").val(data.cpf);
                        $("input[name='numero']").val(data.numero);
                        $("input[name='cidade']").val(data.cidade);
                        $("input[name='email']").val(data.email);
                        $("input[name='bairro']").val(data.bairro);
                        $("input[name='endereco']").val(data.endereco);
                        $("input[name='tel']").val(data.tel);
                        $("input[name='uf']").val(data.uf);
                        $("input[name='id']").val(data.id);
                        $("input[name='CRM']").val(data.crm);
                        $("option[value="+data.esp+"]").attr('selected','selected');
                        $("tr:odd").addClass("odd");
                        $("tr:even").addClass("even");
                    //});
                    loadelement();
                    val();
                } else {

                    $("#alerta").html(data.erro);
                    $("#alerta").dialog("open");
                }
            },
            error: function(data) {
                Alert(data);
            }
        });

        $('img').click(function() {
            alert("");
        });
    }
    function delData(id) {
            
        $.ajax({
            type: "POST",
            dataType: 'json',
            url: "PessoaSev",
            data: "action=del&id=" + id,
            success: function(data) {

                getData();
                if (typeof (data.erro) === 'undefined') {
                    getData();

                } else {
                    getData();
                    delAllLinha();
                    $("#alerta").html(data.erro);
                    $("#alerta").dialog("open");
                    val();
                }
            },
            error: function(data) {
                Alert(data);
            }
        });
    }
    function delAllLinha() {
        $("tbody>tr").remove();

    }
    function loadelement() {
        $('tr').click(function() {
            $(this).find('img:first').click(function(){
                 delData($(this).attr('n'));
                 //exit();
            });
            $(this).find('img:eq(1)').click(function(){
                 getDataOne($(this).attr('n'));
                 //exit();
            });
          
        });

    }
});

